package alarm;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import db.Db;
import db.DeltaRreg;
import db.EVres;
import db.Preg;
import db.WellReg;
import db.skriveTilDb;
import wellsim.no.ntnu.fp.su.Flow;
import wellsim.no.ntnu.fp.su.Well;

public class AlarmTest {
	
	private static Db  dbm = Db.getInstance();
	
	private double EV;
	private double deltaTime = 1.0;
	private String Alarm = "Feil";
	private double EVlim;
	private double deltaRValue;
	double [] wellValues;
	private static boolean update = false;
	private double deltaPressure, deltaTemperature, pOvert;
	
	public void checkColor(){
		//if(System.currentTimeMillis() % 1000 == 0) break;
		
		
	}
	
	
	
	
	private static List<DeltaRreg> deltaR() throws SQLException {
		
		List<DeltaRreg> liste = new ArrayList<DeltaRreg>();
		while(true){
		if(System.currentTimeMillis() % 1000 == 0){ 
			break;
		
		}
		String query = "SELECT deltaR FROM DeltaR ";
		ResultSet rs = dbm.executeQuery(query);
		while(rs.next()){
			double deltaR = rs.getDouble(1);
			liste.add(new DeltaRreg(deltaR));
		}
		//System.out.println(liste);
		//return liste;
		}
		return liste;
	}

	public List<Double> eVAlarm() throws SQLException{
		
		List<Double> EVresultat = new ArrayList<Double>();
		
		while(true){
			if(System.currentTimeMillis() % 1000 == 0) break;
			
			List <DeltaRreg> resultat = deltaR();
			deltaRValue = resultat.get(resultat.size()-1).getDeltaR();
			//System.out.println("siste deltaR "+deltaRValue);
			EV = deltaRValue/deltaTime; //REGNER UT EV
			EVresultat.add((double)EV);
			EVlim = EV/deltaTime; //REGNER UT EVlim	
		String query3 = "INSERT INTO AlarmReg(EV,deltaP,deltaT,pOverT) values("+EV+","+deltaPressure+","+deltaTemperature+","+pOvert+");";
		dbm.executeUpdate(query3);
		
		
		double minTemperature = Flow.minTemperature;
		double maxTemperature = Flow.maxTemperature;
		deltaTemperature = maxTemperature - minTemperature;
		
		double minPressure = Flow.minPressure;
		double maxPressure = Flow.maxPressure;
		deltaPressure = maxPressure - minPressure;
		if(deltaTemperature > deltaPressure){
			System.out.println(Alarm);
		}
		//System.out.println(deltaRValue);
		}
		return EVresultat;
	}

	private static List<Preg> getDeltaP()throws SQLException {
	List<Preg> listeP = new ArrayList<Preg>();
	
	while(true){
		if(System.currentTimeMillis() % 1000 == 0) break;
		System.out.println("hit da?");
		String query = "SELECT P1 FROM SampleWell ";
		ResultSet rs = dbm.executeQuery(query);
		while(rs.next()){
			double P1 = rs.getDouble(1);
			
			listeP.add(new Preg(P1));
		}
		System.out.println(listeP + "her");
		
		double pValueLast = listeP.get(listeP.size()-1).getP1();
		double pValue = listeP.get(listeP.size()-2).getP1();
		
		System.out.println(pValueLast);
		System.out.println(pValue);
		
		double deltaP = pValue-pValueLast;
		System.out.println(deltaP +"test" );
		//return listeP;
	}
	return listeP;
	}
	
	public void pressureTempratureAlarm(Flow flow) throws SQLException{
		//Denne skal sjekke om deltaP/deltaT>deltaP limit-2
		List <Preg> resultat = getDeltaP();
		double x = resultat.get(resultat.size()-1).getP1();
		double y = resultat.get(resultat.size()-2).getP1();
		
		System.out.println(x);
		System.out.println(y);
		
		double minPressure = Flow.minPressure;
		double maxPressure = Flow.maxPressure;
		
		double deltaP = maxPressure - minPressure;
		double deltaP2 = (maxPressure-minPressure)/ deltaTime;
		pOvert = deltaP/deltaTime;
		
		if(pOvert > deltaP2){
			System.out.println(Alarm);
		}
	}
	
	public void pressureAlarm(Flow flow){
		double pressure = flow.getPressure();
		double maxPressure = Flow.maxPressure;
		
		if(pressure > maxPressure){
			System.out.println(Alarm);
		}
	}

	public static void main(String [] args) throws SQLException{
		deltaR();
		getDeltaP();
	new AlarmTest().eVAlarm();

	}
}
